package vip.yyzhu.redisadmin.api.key.controller;

import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import vip.yyzhu.redisadmin.aop.permissions.AuthUnique;
import vip.yyzhu.redisadmin.aop.verifyKeyType.VerifyKeyType;
import vip.yyzhu.redisadmin.jopo.ResData;
import vip.yyzhu.redisadmin.jopo.ResJson;
import vip.yyzhu.redisadmin.redis.RedisList;
import vip.yyzhu.redisadmin.util.Tool;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@RestController
@RequestMapping(value = "/api/key/pub", produces = "application/json;charset=UTF-8")
@SuppressWarnings("all")
public class SubController {
    @Autowired
    RedisList redisList;
    @Autowired
    Tool tool;

    /**
     * 使用游标获取kes列表
     * @param cursor 游标
     * @param size 一页的数量
     * @param match 模糊搜索
     * @return {"map":{"total":10, "sift":5, "cursor":"0"}, "list":[]}
     */
    @AuthUnique
    @RequestMapping(value = "/scan", method = RequestMethod.POST)
    public Object scan(HttpServletRequest request,
                       @RequestParam String cursor,
                       @RequestParam(required = false, defaultValue = "100") int size,
                       @RequestParam(required = false, defaultValue = "100") int page_size,
                       @RequestParam(required = false) String match) {
        val jedis = tool.getJedis(request);

        ScanParams scanParams = new ScanParams();
        scanParams.count(page_size);//页数
        if (match != null && !match.equals("")) {
            scanParams.match(match);
        }

        String new_cursor = cursor;
        List<String> list = new ArrayList();
        //  加载到size的数量
        while (list.size() < size){
            ScanResult<String> scan = jedis.scan(new_cursor, scanParams);
            new_cursor = scan.getStringCursor();
            list.addAll(scan.getResult());
            if(new_cursor.equals("0")){
                break;
            }
        }
        Map map = new HashMap();
        map.put("total", jedis.dbSize());
        map.put("cursor", new_cursor);
        if (match != null && !match.equals("")) {
            map.put("sift", jedis.keys(match).size());
        }else{
            map.put("sift", map.get("total"));
        }
        ResData res = new ResData();
        res.setList(list);
        res.setMap(map);
        return new ResJson(200, "成功", res);
    }

    /**
     * 修改key
     * @param key 旧的key
     * @param new_key 新的key
     * @param cover 是否强行更改
     * @return
     */
    @VerifyKeyType
    @RequestMapping(value = "/rename", method = RequestMethod.POST)
    public Object rename(HttpServletRequest request,
                         @RequestParam String key,
                         @RequestParam String new_key,
                         @RequestParam(defaultValue = "false") Boolean cover) {
        System.out.println("cover = " + cover);
        val jedis = tool.getJedis(request);
        String type = jedis.type(new_key);
        if(!cover && !"none".equals(type)){
            Map map = new HashMap();
            map.put("key", new_key);
            map.put("type", type);
            ResData res = new ResData(map);
            res.setErr("key已存在, 是否继续更新key, 这将覆盖");
            return new ResJson(4101, "key已存在", res);
        }
        String rename = jedis.rename(key, new_key);
        Map map = new HashMap();
        map.put("oldKey", key);
        map.put("newKey", new_key);
        ResData res = new ResData();
        res.setMap(map);
        res.setRedis(rename);
        return new ResJson(200, "成功", res);
    }


    @VerifyKeyType
    @RequestMapping(value = "/expire", method = RequestMethod.POST)
    public Object expire(HttpServletRequest request,
                      @RequestParam String key,
                      @RequestParam int ttl) {
        val jedis = tool.getJedis(request);
        Long redis = jedis.expire(key, ttl);
        Map map = new HashMap();
        map.put("key", key);
        map.put("ttl", ttl);
        ResData res = new ResData();
        res.setMap(map);
        res.setRedis(redis);
        return new ResJson(200, "成功", res);
    }
    @VerifyKeyType
    @RequestMapping(value = "/persist", method = RequestMethod.POST)
    public Object persist(HttpServletRequest request,
                         @RequestParam String key) {
        val jedis = tool.getJedis(request);
        Long redis = jedis.persist(key);
        Map map = new HashMap();
        map.put("key", key);
        ResData res = new ResData();
        res.setMap(map);
        res.setRedis(redis);
        return new ResJson(200, "成功", res);
    }

    @AuthUnique
    @RequestMapping(value = "/selectDb", method = RequestMethod.POST)
    public Object selectDb(HttpServletRequest request,
                          @RequestParam int db) {
        val jedis = tool.getJedis(request);
        String select = jedis.select(db);
        Map map = new HashMap();
        map.put("db", db);
        ResData res = new ResData();
        res.setMap(map);
        res.setRedis(select);
        return new ResJson(200, "成功", res);
    }
}
